﻿using System;
using System.Text.RegularExpressions;
using System.Threading;
using EzDesk.DesktopClient.Event;
using EzDesk.DesktopClient.FunctionWindowModule.ViewContract;
using EzDesk.DesktopClient.MVVMEnhance;
using EzDesk.Generic;
using EzDesk.MVVMBase;
using EzDesk.Utility.Json;
using EzDesk.Utility.Net;
using Microsoft.Practices.Unity;

namespace EzDesk.DesktopClient.ViewModel
{
    public interface IActionViewModel
    {
        string IsSuccess { get; set; }
        string ResultInformation { get; set; }
        void DoAction(string url);
    }

    public class ActionViewModel : ViewModelBase, IActionViewModel
    {
        private readonly IWebRequestExt _webRequestExt;

        private string _isSuccess;

        public string IsSuccess
        {
            get { return _isSuccess; }
            set
            {
                _isSuccess = value;

                RaisePropertyChanged("IsSuccess");
            }
        }

        private string _resultInformation;

        public string ResultInformation
        {
            get { return _resultInformation; }
            set
            {
                _resultInformation = value;
                RaisePropertyChanged("ResultInformation");
            }
        }

        [InjectionConstructor]
        public ActionViewModel(IWebRequestExt webClient)
            : base()
        {
            _webRequestExt = webClient;
            //FunctionNavigateEvent.Instance.Subscribe(this.OnFunctionNavigate);
        }

        public void OnFunctionNavigate(FunctionNavigateArgs args)
        {
            string targetValue = GetTargetType(args);

            if (targetValue != "action") return;

            string urlWithToken = UserManagement.Instance.GetUrlWithUserToken(args.Url);
            DoAction(urlWithToken);
        }

        
        public void DoAction(string url)
        {
            ResultInformation = "正在处理中,请稍等!";

            ThreadPool.QueueUserWorkItem(obj => DoRequest(url));


            IsSuccess = string.Empty;
            ThreadPool.QueueUserWorkItem(obj =>
            {
                Thread.Sleep(5000);
                DataManagement.Instance.RefreshDataFromServer();
            });
        }

        
        private void DoRequest(string url)
        {
            try
            {
                string resultJson = _webRequestExt.GetStringWithPost(url, string.Empty);
                //string resultJson = "{State:'Success',Info:'確定成功！',Data:null}";
                var returnValue = JsonHelper.JsonToDictionary(resultJson);

                IsSuccess = returnValue["Status"] == "Success" ? "操作成功" : "操作失败";
                ResultInformation = returnValue["Info"];
            }
            catch (Exception exp)
            {
                ResultInformation = exp.Message;
                LogEx.ERROR("FunctionModule Action", exp.Message, exp);
                IsSuccess = "操作失败";
                ResultInformation = "服务连接服务器";

            }
        }

        private string GetTargetType(FunctionNavigateArgs args)
        {
            var result = Regex.Match(args.Url, @"target=\w+");
            try
            {
                if (!result.Success) return "page";
                return result.Value.Split('=')[1];
            }
            catch (Exception)
            {
                return "page";
            }
        }
    }
}